package com.example.demo.service.impl;

import com.example.demo.dao.CustomerMapper;
import com.example.demo.pojo.Customer;
import com.example.demo.service.ICustomerService;
import com.example.demo.until.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iCustomerService")
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    CustomerMapper customerMapper;

    //查询用户名和密码
    @Override
    public Customer login(String username, String password) {
        String md5Password = MD5Util.getPwd(password);
        Customer customer = customerMapper.selectByNameAndPassword(username, md5Password);
        return customer;
    }

    //查询用户名
    @Override
    public Customer select(String username) {
        Customer customer = customerMapper.selectByName(username);
        return customer;
    }

    //添加用户
    @Override
    public String addCustomer(Customer customer) {
        String md5Password = MD5Util.getPwd(customer.getPassword());
        customer.setPassword(md5Password);
        int result = customerMapper.insert(customer);
        if (result > 0) {
            return "添加用户成功";
        }
        return "添加用户失败";
    }

    //更新用户信息
    @Override
    public Customer updateByInformation(String username, String email, String phone, String question, String answer) {
        Customer customer = new Customer();
        customerMapper.updateByInformation(username, email, phone, question, answer);
        return customer;
    }
//更新密码
    @Override
    public String updatePassword(String username, String passwordNew) {
        String md5PasswordNew = MD5Util.getPwd(passwordNew);
        int result = customerMapper.updateByPassword(username, md5PasswordNew);
        if (result > 0) {
            return "重置密码成功";
        }
        return "重置密码失败";
    }
}